//#include <GL/gl.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <fstream>
//#include <stdio.h>  /* defines FILENAME_MAX */
//#ifdef _WIN32
//#include <direct.h>
//#define GetCurrentDir _getcwd
//#else
//#include <unistd.h>
//#define GetCurrentDir getcwd
//#endif

//#include "MathUtil.h"
#include "GameData.h"
#include "GameRenderer.h"
#include "state/GameStateManager.h"
#include "gui/FontManager.h"
#include "client/Client.h"
#include "terrain/Chunk.h"
#ifdef DEBUG_NEW
#include "debug/debug_new.h"
#endif
#if defined _WIN32

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
    //int argc = __argc;
    //char** argv = &__argv[0];
#else

int SDL_main(int argc, char** argv) {
#endif
//int main(int argc, char** argv) {
//    char cCurrentPath[FILENAME_MAX];
//
//    if (!GetCurrentDir(cCurrentPath, sizeof (cCurrentPath))) {
//        return errno;
//    }
//
//    printf("The current working directory is %s", cCurrentPath);
//
//
    /*
    const int SCREEN_SIZE[2] = {800, 600};
    const int SCREEN_BPP = 32;

    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_SetVideoMode(SCREEN_SIZE[0], SCREEN_SIZE[1],
            SCREEN_BPP, SDL_OPENGL);

    GameData* data = new GameData(SCREEN_SIZE);

    data->state_manager()->push_state("free");

    SDL_Event evt;
    unsigned int p_tick = SDL_GetTicks();
    int i = 0;
    string s = "";

    while (!data->run_code()) {
        unsigned int ms = SDL_GetTicks() - p_tick;
        p_tick = SDL_GetTicks();
        while (SDL_PollEvent(&evt)) {
            if (evt.type == SDL_QUIT) {
                data->set_run_code(1);
            }
            data->state_manager()->input(&evt, ms);
        }
        data->state_manager()->update(ms);
        data->state_manager()->draw();

        if (i % 30 == 0) {
            char str[50];
            sprintf(str, "FPS: %4.2f", (1000.0f / (float) ms));
            s = str;
            i = 0;
        }
        i++;
        float mw, mh;
        glPushMatrix();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        glOrtho(0, SCREEN_SIZE[0], SCREEN_SIZE[1], 0, -100, 100);
        glColor3f(1.0f, 1.0f, 1.0f);
        data->font()->render(s, 10, 10, 1, &mw, &mh);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
        SDL_GL_SwapBuffers();
    }

    int c = data->run_code();
    c = (c == 1 ? 0 : c);

    delete data;

    SDL_Quit();
    return c;
     */
    
    ifstream f("data\\test.data", ios::in);
    Client c;
    Chunk chunk;
    c.load_chunk(&f, &chunk);
    f.close();
    return 0;
    
}